<template>
  <header class="global-header">
    <div class="header-container">
      <!-- 左侧：Logo和标题 -->
      <div class="header-left">
        <RouterLink to="/" class="logo-link">
          <img class="logo" src="@/assets/logo.png" alt="Logo" />
          <h1 class="site-title">AI零代码生成平台</h1>
        </RouterLink>
      </div>

      <!-- 中间：导航菜单 -->
      <nav class="header-nav">
        <div class="nav-menu">
          <RouterLink
            v-for="item in visibleMenuItems"
            :key="item?.key"
            :to="item?.key as string"
            class="nav-item"
            :class="{ active: selectedKeys.includes(item?.key as string) }"
          >
            <component v-if="item?.icon" :is="item.icon" class="nav-icon" />
            {{ item?.label }}
          </RouterLink>
        </div>
      </nav>

      <!-- 右侧：用户操作区域 -->
      <div class="header-right">
        <div v-if="loginUserStore.loginUser.id" class="user-info">
          <a-dropdown>
            <div class="user-dropdown">
              <a-avatar :src="loginUserStore.loginUser.userAvatar" />
              <span class="username">{{ loginUserStore.loginUser.userName ?? '无名' }}</span>
            </div>
            <template #overlay>
              <a-menu>
                <a-menu-item @click="doLogout">
                  <LogoutOutlined />
                  退出登录
                </a-menu-item>
              </a-menu>
            </template>
          </a-dropdown>
        </div>
        <div v-else>
          <GlassButton type="primary" size="small" @click="() => router.push('/user/login')">
            登录
          </GlassButton>
        </div>
      </div>
    </div>
  </header>
</template>

<script setup lang="ts">
import { computed, h, ref } from 'vue'
import { useRouter } from 'vue-router'
import { type MenuProps, message } from 'ant-design-vue'
import { useLoginUserStore } from '@/stores/loginUser.ts'
import { userLogout } from '@/api/userController.ts'
import { LogoutOutlined, HomeOutlined } from '@ant-design/icons-vue'
import GlassButton from '@/components/base/GlassButton.vue'

interface MenuItem {
  key: string
  icon?: () => any
  label: string
  title: string
}

const loginUserStore = useLoginUserStore()
const router = useRouter()
// 当前选中菜单
const selectedKeys = ref<string[]>(['/'])
// 监听路由变化，更新当前选中菜单
router.afterEach((to, from, next) => {
  selectedKeys.value = [to.path]
})

// 菜单配置项
const originItems: MenuItem[] = [
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/admin/userManage',
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: '/admin/appManage',
    label: '应用管理',
    title: '应用管理',
  },
  {
    key: '/admin/chatManage',
    label: '对话管理',
    title: '对话管理',
  },

]

// 过滤菜单项
const filterMenus = (menus: MenuItem[] = []) => {
  return menus.filter((menu) => {
    const menuKey = menu.key
    if (menuKey.startsWith('/admin')) {
      const loginUser = loginUserStore.loginUser
      if (!loginUser || loginUser.userRole !== 'admin') {
        return false
      }
    }
    return true
  })
}

// 展示在菜单的路由数组
const visibleMenuItems = computed(() => filterMenus(originItems))

// 退出登录
const doLogout = async () => {
  const res = await userLogout()
  if (res.data.code === 0) {
    loginUserStore.setLoginUser({
      userName: '未登录',
    })
    message.success('退出登录成功')
    await router.push('/user/login')
  } else {
    message.error('退出登录失败，' + res.data.message)
  }
}
</script>

<style scoped>
.global-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  background:
    radial-gradient(ellipse 800px 200px at 50% 0%, rgba(59, 130, 246, 0.08) 0%, transparent 50%),
    linear-gradient(135deg,
      rgba(15, 23, 42, 0.95) 0%,
      rgba(30, 41, 59, 0.9) 25%,
      rgba(51, 65, 85, 0.85) 50%,
      rgba(71, 85, 105, 0.8) 100%
    );
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  overflow: hidden;
}

.global-header::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background:
    radial-gradient(circle at 20% 50%, rgba(255, 255, 255, 0.03) 1px, transparent 1px),
    radial-gradient(circle at 80% 50%, rgba(255, 255, 255, 0.02) 1px, transparent 1px);
  background-size: 100px 100px, 150px 150px;
  pointer-events: none;
  animation: headerSparkle 30s ease-in-out infinite;
}

.header-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 24px;
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.header-left {
  display: flex;
  align-items: center;
}

.logo-link {
  display: flex;
  align-items: center;
  gap: 12px;
  text-decoration: none;
  transition: all 0.3s ease;
}

.logo-link:hover {
  transform: scale(1.02);
}

.logo {
  height: 40px;
  width: 40px;
  border-radius: 8px;
}

.site-title {
  margin: 0;
  font-size: 18px;
  color: #ffffff;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
  font-weight: 700;
  white-space: nowrap;
  letter-spacing: 0.5px;
  background: linear-gradient(135deg, #ffffff 0%, #e2e8f0 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.header-nav {
  flex: 1;
  display: flex;
  justify-content: center;
  max-width: 600px;
}

.nav-menu {
  display: flex;
  gap: 8px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  color: rgba(255, 255, 255, 0.8);
  text-decoration: none;
  border-radius: 8px;
  font-weight: 500;
  font-size: 14px;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.nav-item::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.1), transparent);
  transition: left 0.5s;
}

.nav-item:hover::before {
  left: 100%;
}

.nav-item:hover {
  color: #ffffff;
  background: rgba(255, 255, 255, 0.1);
  transform: translateY(-1px);
}

.nav-item.active {
  color: #60a5fa;
  background: rgba(96, 165, 250, 0.1);
  border: 1px solid rgba(96, 165, 250, 0.2);
}

.nav-icon {
  font-size: 16px;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  color: #ffffff;
}

.user-dropdown {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.user-dropdown:hover {
  background: rgba(255, 255, 255, 0.1);
}

.username {
  font-weight: 500;
  color: rgba(255, 255, 255, 0.9);
}



@keyframes headerSparkle {
  0%, 100% {
    opacity: 0.8;
    transform: translateX(0);
  }
  50% {
    opacity: 0.4;
    transform: translateX(10px);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-container {
    padding: 0 16px;
  }

  .site-title {
    font-size: 16px;
  }

  .nav-menu {
    gap: 4px;
  }

  .nav-item {
    padding: 6px 12px;
    font-size: 13px;
  }
}
</style>
